package cn.itsource.pethome.system.dictionary.service.imp;

import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.system.dictionary.domain.DictionaryType;
import cn.itsource.pethome.system.dictionary.mapper.DictionaryTypeMapper;
import cn.itsource.pethome.system.dictionary.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author:R
 * @Date:2023/1/5 19:39
 * Description
 */
@Service
public class DictionaryTypeServiceImpl extends BaseServiceImpl<DictionaryType> implements IDictionaryTypeService {
    @Autowired
    private DictionaryTypeMapper typeMapper;

    @Transactional
    @Override
    public void delete(Long id) {
        if (typeMapper.findDetail(id) == 0){
            typeMapper.delete(id);
        }else {
            throw new RuntimeException("当前数据字典中包含明细，不能删除");
        }
    }

    @Transactional
    @Override
    public void batchDel(List<Long> ids) {
        for (Long id : ids) {
            Long detail = typeMapper.findDetail(id);
            if (detail != 0){
                throw new RuntimeException("数据字典：【"+typeMapper.findOne(id).getName()+"】中包含明细，不能删除。请重新选择");
            }
        }
        typeMapper.batchDel(ids);
    }

    @Transactional
    @Override
    public void add(DictionaryType dictionaryType) {
        Long sn = typeMapper.findSn(dictionaryType);
        if (sn == 0){
            typeMapper.add(dictionaryType);
        }else {
            throw new RuntimeException("未保证当前字典名和编号唯一,不能添加");
        }
    }

    @Transactional
    @Override
    public void update(DictionaryType dictionaryType) {
        Long sn = typeMapper.findSn(dictionaryType);
        if (sn == 0){
            typeMapper.update(dictionaryType);
        }else {
            throw new RuntimeException("未保证当前字典名和编号唯一,修改失败");
        }
    }
}
